package com.zhiche.lisa.bms.controller.utils;

import com.google.common.base.Strings;
import com.zhiche.lisa.bms.pojo.vo.common.AttachDetailVO;
import com.zhiche.lisa.bms.service.utils.common.UploadUtil;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * @Author: liuanshun
 * @Description:
 * @Date: Create in 14:03 2018/9/28
 */
@RestController
@RequestMapping("/upload")
@Api(value = "/upload", description = "文件上传", tags = {"文件上传接口"})
public class UploadController {
	private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

	@Autowired
	private UploadUtil uploadUtil;

	/**
	 * 下载资源
	 *
	 * @param key 资源key
	 * @return 下载资源流
	 * @throws IOException
	 */
	@ApiOperation(value = "获取文件下载路径", notes = "获取下载路径", httpMethod = "GET")
	@GetMapping("/getFileDownloadLocation")
	public RestfulResponse<AttachDetailVO> exportResouce(@RequestParam("key") String key, @RequestParam(value = "fileName", required = false) String fileName) {
		RestfulResponse<AttachDetailVO> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
		try {
			if (Strings.isNullOrEmpty(key)) {
				restful = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
			} else {
				AttachDetailVO attachDetailVO = new AttachDetailVO();
				attachDetailVO.setAttachKey(key);
				if (!Strings.isNullOrEmpty(fileName)) {
					attachDetailVO.setAttachName(fileName);
				}
				restful.setData(uploadUtil.addAttachUploadResult(attachDetailVO));
			}
		} catch (Exception e) {
			restful = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
		}
		return restful;
	}

	/**
	 * 下载资源
	 *
	 * @param condition 资源key
	 * @return 下载资源流
	 * @throws IOException
	 */
	@ApiOperation(value = "获取文件下载路径", notes = "获取下载路径", httpMethod = "POST")
	@PostMapping("/getFileDownloadLocation")
	public RestfulResponse<AttachDetailVO> exportResouce(@RequestBody Map<String, Object> condition) {
		RestfulResponse<AttachDetailVO> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
		if (CollectionUtils.isEmpty(condition)) {
			restful = new RestfulResponse<>(CommonEnum.ERROR.getCode(), "无绑定信息", null);
		} else {
			try {
				AttachDetailVO attachDetailVO = new AttachDetailVO();
				String key = (String) condition.get("fileKey");
				if (!Strings.isNullOrEmpty(key)) {
					attachDetailVO.setAttachKey(key);
				}
				String fileName = (String) condition.get("fileName");
				if (!Strings.isNullOrEmpty(fileName)) {
					attachDetailVO.setAttachName(fileName);
				}
				restful.setData(uploadUtil.addAttachUploadResult(attachDetailVO));
			} catch (Exception e) {
				restful = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
			}
		}
		return restful;
	}
}
